package com.lanou3g.ptp.achievement.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.entity.Diary;
import com.lanou3g.ptp.achievement.mapper.DiaryMapper;
import com.lanou3g.ptp.achievement.service.DiaryService;
import com.lanou3g.ptp.entity.SignInDate;
import com.lanou3g.ptp.signInDate.service.SignInDateService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class DiaryServiceImpl implements DiaryService {

    @Autowired
    private DiaryMapper diaryMapper;

    @Reference
    private SignInDateService signInDateService;
    //字符串处理成数组
    private List<Diary> forThis(List<Diary> diaries){
        for (Diary diary : diaries) {
            String img = diary.getImg();
            //分割字符串
            String[] imgs;
            String meter = ",";
            imgs = img.split(meter);
            diary.setImgs(imgs);
            diary.setImg(null);
        }
        return diaries;
    }



    @Override
    public PageInfo<Diary> findAllDiary(Integer page, Integer size) {

        PageHelper.startPage(page, size);
        List<Diary> all = diaryMapper.findAll();
        List<Diary> diaries = forThis(all);
        return new PageInfo<>(diaries);

    }

    @Override
    public List<Diary> findAll1Diary() {
        List<Diary> all = diaryMapper.findAll1();
        return forThis(all);
    }

    @Override
    public PageInfo<Diary> findByKeyword(Integer page, Integer size, String keyword) {
        PageHelper.startPage(page, size);
        List<Diary> byKeyword = diaryMapper.findByKeyword("%" + keyword + "%");
        List<Diary> diaries = forThis(byKeyword);
        return new PageInfo<>(diaries);

    }

    @Override
    public Diary findDiaryById(Long id) {
        Diary diary = diaryMapper.findDiaryById(id);
        String img = diary.getImg();
        //分割字符串
        String[] imgs;
        String meter = ",";
        imgs = img.split(meter);
        diary.setImgs(imgs);
        diary.setImg(null);
        return diary;

    }

    @Override
    public boolean insertDiary(Diary diary) {

        Date date = new Date();
        diary.setCreateTime(date);
        diary.setUpdateTime(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        Date time1 = cal.getTime();
        cal.get(Calendar.DAY_OF_YEAR);
        cal.add(Calendar.DATE,1);
        Date time2 = cal.getTime();

        if(ifTodayInSignDates(diary.getTaskId())){
            Diary diary2 = diaryMapper.find1(diary.getStudentId(), diary.getTaskId(), time1, time2);
            if (diary2!=null){
                int i = diaryMapper.insert(diary);
                return i>0;
            }
        }
        return false;
    }

    private boolean ifTodayInSignDates(Long taskId) {
        Date date = new Date();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(date);

        List<SignInDate> signTime = signInDateService.findSignTime(taskId);
        for (SignInDate signInDate : signTime) {
            if (format.format(signInDate.getDate()).equals(format1)){
                return true;
            }


        }

        return false;
    }

    @Override
    public void updateDiary(Long id, Diary diary) {

        diary.setUpdateTime(new Date());
        diaryMapper.updateDiary(id, diary);

    }

    @Override
    public void deleteDiaryId(Long id) {

        diaryMapper.deleteDiaryId(id);
    }

    @Override
    public void deleteDiaryIds(Long[] ids) {

        diaryMapper.deleteDiaryIds(ids);
    }

    @Override
    public List<Diary> MyFindAll(Long id) {
        List<Diary> diaryList = diaryMapper.myFindAll(id);
        return forThis(diaryList);
    }

    @Override
    public List<Diary> findDiarySituation(Date date,Long taskId) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        Date time1 = cal.getTime();
        cal.get(Calendar.DAY_OF_YEAR);
        cal.add(Calendar.DATE,1);
        Date time2 = cal.getTime();
        List<Diary> diarySituation = diaryMapper.findDiarySituation(time1, time2, taskId);
        return forThis(diarySituation);
    }

    @Override
    public Diary findByTime(Date date, Long taskId,Long studentId) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        Date time1 = cal.getTime();
        cal.get(Calendar.DAY_OF_YEAR);
        cal.add(Calendar.DATE,1);
        Date time2 = cal.getTime();
        Diary diaryMapper1 = diaryMapper.find1(studentId, taskId, time1, time2);
        String img = diaryMapper1.getImg();
        //分割字符串
        String[] imgs;
        String meter = ",";
        imgs = img.split(meter);
        diaryMapper1.setImgs(imgs);
        diaryMapper1.setImg(null);
        return diaryMapper1;
    }
}
